//
//  BarChartView.swift
//  HuaYun
//
//  Created by enghou on 2018/7/20.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

import UIKit

import SnapKit

import Charts

class BarChart: UIView,ChartViewDelegate {
    
    let barChart = BarChartView()
    
    func configBarChart()->Void{
        
        barChart.delegate = self
        
        self.addSubview(barChart)
        
        self.barChart.snp.makeConstraints { (make) in
            
            make.size.equalTo(self.size)
            
            make.center.equalTo(self)
            
        }
        
        self.barChart.noDataText = "暂无数据"
        
        self.barChart.drawValueAboveBarEnabled = false
        
        self.barChart.highlightPerTapEnabled = true
        
        self.barChart.drawBarShadowEnabled = true
        
        self.barChart.scaleXEnabled = false
//
        self.barChart.doubleTapToZoomEnabled = false
        
        self.barChart.dragEnabled = false
        
        self.barChart.dragDecelerationEnabled = false
        
//        self.barChart.dragDecelerationFrictionCoef =
        
        self.barChart.data = self.setData()
        
        self.barChart.animate(yAxisDuration: TimeInterval(1.0))
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        self.configBarChart()
        
    }
    
    init() {
        
        super.init(frame: CGRect.zero)
        
        self.configBarChart()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    
    func setData() -> BarChartData {
        
        let xVals_count = 12
        
        let maxYVal = 100
        
        var xVals: Array<String> = []
        
        for i in 0 ..< xVals_count {
            xVals.append(String(i + 1) + "月")
        }
        
        var yVals: Array<BarChartDataEntry> = []
        
        for i in 0 ..< 12 {
            
            let mult = maxYVal + 1
            
            let val = Double(arc4random_uniform(UInt32(mult)))
            
            let entry = BarChartDataEntry.init(x: Double(i), y: val)
            
            
            
            yVals.append(entry)
            
        }
        
        let set = BarChartDataSet.init(values: yVals, label: nil)
        
        set.drawValuesEnabled = true
        
        set.highlightEnabled = true
        
        set.setColors(UIColor.red,UIColor.blue,UIColor.green)
        
        var dataSets: Array<BarChartDataSet> = []
        
        dataSets.append(set)
        
        let data = BarChartData.init(dataSets: dataSets)
        
        data.setValueFont(UIFont.systemFont(ofSize: 10.0))
        
        data.setValueTextColor(UIColor.red)
        
//        let formatter = NumberFormatter()
//
//        formatter.numberStyle = .decimal
//
//        formatter.positiveFormat = "#0.0"
//
//        data.setValueFormatter(formatter as! IValueFormatter)
        
        return data
        
    }
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
